//
//  BTAccout.m
//  WeChat
//
//  Created by 互联网 on 16/10/28.
//  Copyright © 2016年 bigtree. All rights reserved.
//

#import "BTAccout.h"


#define  kUserKey @"user"
#define  kPwdKey @"psw"
#define  kLoginKey @"login"

/**
 *  在内存中只创建一次
 */
static NSString *domain = @"teacher.local";
static NSString *host = @"127.0.0.1";
static int port = 5222;

@interface BTAccout()

@end

@implementation BTAccout

+(instancetype)shareAccount
{
    return [[self alloc] init];
}
#pragma mark 分配内存创建对象
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    NSLog(@"%s",__func__);
    static BTAccout *account;
    
    /**
     *  如果三个线程同时调用该方法，就会同时创建三个对象
     */
//    if (account == nil) {
//        account = [super allocWithZone:zone];
//    }
    // 为了线程安全
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 这句代码只会执行一次
        account = [super allocWithZone:zone];
        
#warning 从沙盒获取上次用户登录信息
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        account.loginUser = [defaults objectForKey:kUserKey];
        account.loginPsw = [defaults objectForKey:kPwdKey];
        account.login = [defaults boolForKey:kLoginKey];
    });
    return account;
}

-(void)saveToSandBox
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.loginUser forKey:kUserKey];
    [defaults setObject:self.loginPsw forKey:kPwdKey];
    [defaults setBool:self.isLogin forKey:kLoginKey];
    [defaults synchronize];
}

-(NSString *)domain
{
    return domain;
}
-(NSString *)host
{
    return host;
}
-(int)port
{
    return port;
}

@end
